//
//  DXPageContentView.swift
//  SwiftDemo
//
//  Created by Steven on 2020/8/5.
//  Copyright © 2020 Steven. All rights reserved.
//

import Foundation
import UIKit
protocol DXContenteViewProtocol:class {
    func pageContentViewDidScroll(fromIndex:NSInteger,toIndex:NSInteger)
}


private let  colletionCell_id = "DXMenuTitleViewCellId"

class DXPageContentView: UIView{

    
    var subVCArr:Array<UIViewController>!
    private var parentVC:UIViewController!
    private var startOffsetX:CGFloat = 0
    weak var pageContentViewDelegate:DXContenteViewProtocol?
    
    lazy var colletionView:UICollectionView = {
        var layout = UICollectionViewFlowLayout()
        layout.itemSize = self.bounds.size
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        layout.scrollDirection  = .horizontal
        
        let colletionV = UICollectionView(frame: self.bounds, collectionViewLayout: layout)
        colletionV.bounces = false
        colletionV.delegate = self
        colletionV.dataSource = self
        colletionV.isPagingEnabled = true
        colletionV.backgroundColor = .white
        colletionV.register(UICollectionViewCell.self, forCellWithReuseIdentifier: colletionCell_id)
        return colletionV
    }()
    
    var currentIndex = 0{
        didSet{
            if currentIndex < 0 || currentIndex > subVCArr.count {
                return
            }
            colletionView.scrollToItem(at: IndexPath(item: currentIndex, section: 0), at: .centeredHorizontally, animated: true)
        }
    }
    
    convenience  init(frame: CGRect,subVCArr:[UIViewController],parentVC:UIViewController) {
        self.init(frame:frame)
        self.subVCArr = subVCArr
        self.parentVC = parentVC
        self.addSubview(self.colletionView)
        
       let _ = subVCArr.map { (vc) -> Void in
            self.parentVC.addChild(vc)
        }
    }
    
}

extension DXPageContentView:UICollectionViewDelegate,UICollectionViewDataSource{
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return subVCArr.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: colletionCell_id, for: indexPath)
        let lab:UILabel = {
            let lab = UILabel()
            cell.addSubview(lab)
            lab.snp.makeConstraints { (make) in
                make.edges.equalTo(cell);
            }
            return lab
        }()
        lab.text = "text"

        return cell
    }
    func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
        startOffsetX = scrollView.contentOffset.x
    }
    
    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        let scrollVW = scrollView.dx_width
        let currentOffsetX = scrollView.contentOffset.x
        let startIndex = floor(startOffsetX/scrollVW)
        let endIndex = floor(currentOffsetX/scrollVW)
        if pageContentViewDelegate != nil{
            pageContentViewDelegate?.pageContentViewDidScroll(fromIndex: NSInteger(startIndex), toIndex: NSInteger(endIndex))
        }
    }
}
